package com.karolak.math.view;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.Timer;

import org.jfree.ui.RefineryUtilities;

import com.karolak.data.DataProvider;

public class ChartFrame extends ChartInd {

	private static final long serialVersionUID = 1L;

	private static final String START = "Start";
	private static final String STOP = "Stop";

	private static final int VERY_FAST = 1;
	private static final int FAST = 100;
	private static final int SLOW = 5 * FAST;

	private DataProvider dataProvider;

	private Timer timer;

	public ChartFrame(String title) {
		super(title);
	}

	protected void init() {
		setDataProvider(new DataProvider());
		super.init();
		initElements();
	}

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				ChartFrame frame = new ChartFrame("hw");
				frame.pack();
				RefineryUtilities.centerFrameOnScreen(frame);
				frame.setVisible(true);
				// demo.start();
			}
		});
	}
	
	@Override
	protected void createDataSets() {
		initChartSizeData();
		super.createDataSets();
	}
	
	protected void initChartSizeData() {
		for (int i = 0; i < getChartSize(); i++) {
			float last = (float) getDataProvider().getLast();
			getDataManager().update(last);
		}		
	}
	
	

	protected void initElements() {

		final JComboBox combo = new JComboBox();
		combo.addItem("VeryFast");
		combo.addItem("Fast");
		combo.addItem("Slow");
		combo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if ("VeryFast".equals(combo.getSelectedItem())) {
					getTimer().setDelay(VERY_FAST);
				} else if ("Fast".equals(combo.getSelectedItem())) {
					getTimer().setDelay(FAST);
				} else {
					getTimer().setDelay(SLOW);
				}
			}
		});

		final JButton buttonRun = new JButton(START);
		buttonRun.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				String cmd = e.getActionCommand();
				if (STOP.equals(cmd)) {
					getTimer().stop();
					buttonRun.setText(START);
				} else {
					getTimer().start();
					buttonRun.setText(STOP);
				}
			}
		});

		JPanel btnPanel = new JPanel(new FlowLayout());
		btnPanel.add(buttonRun);
		btnPanel.add(combo);
		this.add(btnPanel, BorderLayout.SOUTH);
		this.timer = new Timer(VERY_FAST, new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				double y = getDataProvider().getLast();
				getDataManager().update(y);
				updateDataSets();
				if (!getDataProvider().isMoreData()) {
					buttonRun.setText(STOP);
					buttonRun.doClick();
				}
			}
		});
	}

	protected DataProvider getDataProvider() {
		return dataProvider;
	}

	protected Timer getTimer() {
		return timer;
	}

	protected void setDataProvider(DataProvider dataProvider) {
		this.dataProvider = dataProvider;
	}
}
